<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">药品编码：</label>
            <div class="layui-input-block">
                <input type="text" name="drugsCode" lay-verify="required" lay-reqtext="药品编码不能为空" placeholder="请输入药品编码"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">药品名称：</label>
            <div class="layui-input-block">
                <input type="text" name="drugsName" lay-verify="required" lay-reqtext="药品名称不能为空" placeholder="请输入药品名称"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">药品规格：</label>
            <div class="layui-input-block">
                <input type="text" name="drugsFormat" lay-verify="required" lay-reqtext="药品规格不能为空" placeholder="请输入药品规格"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">包装单位：</label>
            <div class="layui-input-block">
                <input type="text" name="drugsUnit" lay-verify="required" lay-reqtext="包装单位不能为空" placeholder="请输入包装单位"
                    value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">生产厂家：</label>
            <div class="layui-input-block">
                <input type="text" name="manufacturer" lay-verify="required" lay-reqtext="生产厂家不能为空"
                    placeholder="请输入生产厂家" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">药品剂型：</label>
            <div class="layui-input-block">
                <select name="drugsDosageId" lay-verify="required">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">药品类型：</label>
            <div class="layui-input-block">
                <select name="drugsTypeId" lay-verify="required">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">药品单价（分）：</label>
            <div class="layui-input-block">
                <input type="number" name="drugsPrice" lay-verify="required" lay-reqtext="药品单价不能为空"
                    placeholder="请输入药品单价" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">拼音助记码：</label>
            <div class="layui-input-block">
                <input type="text" name="mnemonicCode" lay-verify="required" lay-reqtext="拼音助记码不能为空"
                    placeholder="请输入拼音助记码" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

        $.getJSON("/api/select/constItem.json", { constantTypeId: 11 }, function (data) {
            data.forEach(e => {
                $("select[name=drugsDosageId]").append("<option value='" + e.id + "'>" + e.name + "</option>");
            });
            form.render();
        });
        $.getJSON("/api/select/constItem.json", { constantTypeId: 10 }, function (data) {
            data.forEach(e => {
                $("select[name=drugsTypeId]").append("<option value='" + e.id + "'>" + e.name + "</option>");
            });
            form.render();
        });
        /**
        * 初始化表单，要加上，不然刷新部分组件可能会不加载
        */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index1 = layer.load(2);
            $.get("/api/drugs/add.json", data.field, function (data) {
                layer.close(index1);
                if (data.code == 0) {
                    var index = layer.alert(data.msg, {
                        title: '错误信息',
                        icon: 2
                    });
                } else {
                    layer.close(parentIndex);
                }
            }, "json");
            return false;
        });

    });
</script>